from PyQt5.QtWidgets import QGraphicsItem
from PyQt5.QtCore import Qt

from GUI import MainWindow
from GUI.Widget import ImageWindow
from GUI.Widget.ToolBar import ToolButton

# globals
Window: MainWindow = None
Tool: ToolButton = None


def init(window, tool: ToolButton):
    global Window
    global Tool
    Window = window
    Tool = tool
    # 工具焦点事件
    tool.toolFocusOffLayerEvent = toolFocusOffLayerEvent
    tool.toolFocusOnLayerEvent = toolFocusOnLayerEvent
    # 工具开关事件
    tool.deselectToolEvent = deselectToolEvent
    tool.selectToolEvent = selectToolEvent


def toolFocusOffLayerEvent(tool, layer):
    """
    逝去工具焦点
    :param tool:
    :param layer:
    :return:
    """
    graphicsItem = layer.graphicsItem
    # 不可移动
    graphicsItem.setFlag(QGraphicsItem.ItemIsMovable, False)


def toolFocusOnLayerEvent(tool, layer):
    """
    获得工具焦点
    :param tool:
    :param layer:
    :return:

    """
    graphicsItem = layer.graphicsItem
    # 可移动
    graphicsItem.setFlag(QGraphicsItem.ItemIsMovable)


def deselectToolEvent(tool):
    """
    取消选择工具事件
    :param tool:
    :return:
    """
    win = Window
    imageWindow: ImageWindow = win.currentImageWindow
    view = imageWindow.view
    view.setCursor(Qt.ArrowCursor)
    layer = win.currentLayer
    layer.graphicsItem.setFlag(QGraphicsItem.ItemIsMovable, False)  # 可移动
    print('关闭工具：', tool.name)


def selectToolEvent(tool):
    """
    选择工具事件
    :param tool:
    :return:
    """
    win = Window
    imageWindow: ImageWindow = win.currentImageWindow
    view = imageWindow.view
    view.setCursor(Qt.SizeAllCursor)
    print('打开工具：', tool.name)
